package 领扣算法.剑指offer.链表中的两数相加;

import java.util.LinkedList;

public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        LinkedList<Integer> data1=new LinkedList<>();
        LinkedList<Integer> data2=new LinkedList<>();
        while (l1!=null) {
            data1.add(l1.val);
            l1=l1.next;
        }
        while(l2!=null){
            data2.add(l2.val);
            l2=l2.next;
        }
        LinkedList<ListNode> res=new LinkedList<>();
        int release=0;
        while(!(data1.isEmpty()&&data2.isEmpty())){
            int a=0;
            int b=0;
            if(!data1.isEmpty()){
                a=data1.pollLast();
            }
            if(!data2.isEmpty()){
                b=data2.pollLast();
            }
            release=(a+b+release);
            res.addFirst(new ListNode(release%10));
            release/=10;
        }
        if (release!=0) {
            res.addFirst(new ListNode(release));
        }
        ListNode hNode=res.pollFirst();
        ListNode middle=hNode;
        while(!res.isEmpty()){
            middle.next=res.pollFirst();
            middle=middle.next;
        }
        return hNode;
    }


     class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
    
}
